#include "devuart.hpp"
#include "sysapi.hpp"

#define LOG_TAG "driver_devuart"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h> 

rt_err_t rx_call(rt_device_t dev, rt_size_t size)
{
    robot_devuart*  p = (robot_devuart*)get_this_from_dev(dev);
    if (size > 0)
    {
        rt_sem_release(&p->irq_rx_sem);
    }
    return RT_EOK;
}

robot_devuart::robot_devuart(const char* name, uint32_t boud)\
    :robot_driver(name), boudHz(boud)
{
    config_uart();
}

void robot_devuart::config_uart()
{
    struct serial_configure crtlinfo_uart_config = RT_SERIAL_CONFIG_DEFAULT;

    crtlinfo_uart_config.baud_rate = boudHz;
    crtlinfo_uart_config.data_bits = 8;
    crtlinfo_uart_config.stop_bits = 1;
    crtlinfo_uart_config.bufsz = 256;
    crtlinfo_uart_config.parity = PARITY_NONE;
    crtlinfo_uart_config.bit_order = BIT_ORDER_LSB;

    rt_device_control(this->hw_handle, RT_DEVICE_CTRL_CONFIG, &crtlinfo_uart_config);
}

void robot_devuart::open()
{
    rt_device_open(hw_handle, RT_DEVICE_FLAG_INT_RX);

    /* for callback */
    rt_device_set_rx_indicate(hw_handle, rx_call);
    add_this_to_dev(hw_handle, this);
}
